class RoleScene extends egret.DisplayObjectContainer {
	public constructor() {
		super();
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
	}

	private onAddToStage(et:egret.TouchEvent) {
		this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
		this.createSubviews();
	}

	private hero:Plane; // 主角
	private enemyFighterArr:Plane[]; // 敌机
	private enemyFighterTimer:egret.Timer;	// 敌机生成计时器

	private lastTime:number;

	private heroBulletArr:Bullet[] = []; //
	private enemyBulletArr:Bullet[] = []; //

	private sound:egret.Sound;
	private soundChannel:egret.SoundChannel;

	private createSubviews() {

		// 生成主角
		this.hero = new Plane("playerFrame1_png", SettingManager.shareManger().heroBulletSpeed);
		this.hero.x = (this.stage.stageWidth - this.hero.width) / 2;
		this.hero.y = this.stage.stageHeight - this.hero.height - 50;
		this.addChild(this.hero);
		this.hero.turnOnTouchEnabled(true);
		this.hero.fire();

		//
		// this.touchEnabled = true;
		// this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);


		// 生成敌机
		this.enemyFighterArr = [];

		this.enemyFighterTimer = new egret.Timer(SettingManager.shareManger().enemyCreateInterval);

		//
		this.sound = RES.getRes("getbomb_mp3");
	}


	// private onTouchMove(et:egret.TouchEvent) {
	// 	if (et.type == egret.TouchEvent.TOUCH_MOVE) {
	// 		var tx:number = et.localX;
	// 		tx = Math.max(0, tx);
	// 		tx = Math.min(this.stage.stageWidth - this.hero.width, tx);
	// 		this.hero.x = tx;
	// 	}
	// }

	private onCreateEnemyFighter(et:egret.Event) {

		var enemy:Plane = Plane.produce("p1_png", SettingManager.shareManger().enemyBulletSpeed);
		enemy.x = Math.random() * (this.stage.stageWidth - enemy.width);
		enemy.y = - Math.random() * SettingManager.shareManger().enemyBirthY;
		this.addChildAt(enemy, this.numChildren - 1);
		this.enemyFighterArr.push(enemy);
		
		enemy.addEventListener("CreateBullet", this.onCreateBullet, this);
		enemy.fire();
		
	}

	private onCreateBullet(et:egret.Event) {
		if (et.currentTarget == this.hero) {
			var bullet:Bullet = Bullet.product("b1_png");
			bullet.x = this.hero.x + (this.hero.width - bullet.width) / 2;
			bullet.y = this.hero.y - bullet.height - 10;
			this.addChildAt(bullet, this.numChildren - 1 - this.enemyFighterArr.length);
			this.heroBulletArr.push(bullet);

		} else {
			var bullet:Bullet = Bullet.product("e1_png");
			var fighter:Plane = et.currentTarget;
			bullet.x = fighter.x + (fighter.width - bullet.width) / 2;
			bullet.y = fighter.y + fighter.height + 10;
			this.addChildAt(bullet, this.numChildren - 1 - this.enemyFighterArr.length);
			this.enemyBulletArr.push(bullet);
		}
	}


	public roleSceneStart() {
		// 开始更新
		this.removeEventListener(egret.Event.ENTER_FRAME, this.updateAllViews, this);
		this.addEventListener(egret.Event.ENTER_FRAME, this.updateAllViews, this);
		this.lastTime = egret.getTimer();
		
		// 开始生成敌机
		this.enemyFighterTimer.removeEventListener(egret.TimerEvent.TIMER, this.onCreateEnemyFighter, this);
		this.enemyFighterTimer.addEventListener(egret.TimerEvent.TIMER, this.onCreateEnemyFighter, this);
		this.enemyFighterTimer.start();

		// 监听子弹事件
		this.hero.addEventListener("CreateBullet", this.onCreateBullet, this);

	}


	public roleSceneStop() {
		// 停止更新
		this.removeEventListener(egret.Event.ENTER_FRAME, this.updateAllViews, this);

		// 停止生成敌机
		this.enemyFighterTimer.stop();
		this.enemyFighterTimer.removeEventListener(egret.TimerEvent.TIMER, this.onCreateEnemyFighter, this);

		// 监听子弹事件
		this.hero.removeEventListener("CreateBullet", this.onCreateBullet, this);

	}



	// 更新场景里面所有的飞机
	private updateAllViews(et:egret.Event) {

		var now:number = egret.getTimer();
		var fps:number = 1000/(now - this.lastTime);
		this.lastTime = now;
		var speedOffset:number = 60/fps;

		var delArr:any[] = [];

		for (var i = 0; i < this.enemyFighterArr.length; i++) {
			var fighter:Plane = this.enemyFighterArr[i];
			fighter.y += speedOffset * SettingManager.shareManger().enemyFighterMoveRate;
			if (fighter.y > this.stage.stageHeight) {
				delArr.push(fighter);
			}
		}

		for (var i = 0; i < delArr.length; i++) {
			var fighter:Plane = delArr[i];
			fighter.stopFire();
			fighter.removeEventListener("CreateBullet", this.onCreateBullet, this);
			
			this.removeChild(fighter);
			Plane.reclaim(fighter);
			this.enemyFighterArr.splice(this.enemyFighterArr.indexOf(fighter), 1);
		}


		delArr = [];

		for (var i = 0; i < this.heroBulletArr.length; i++) {
			var bullet:Bullet = this.heroBulletArr[i];
			bullet.y -= speedOffset * SettingManager.shareManger().heroBulletMoveRate;
			if (bullet.y < -bullet.height) {
				delArr.push(bullet);
			}
		}

		for (var i = 0; i < delArr.length; i++) {
			var bullet:Bullet = delArr[i];
			
			this.removeChild(bullet);
			this.heroBulletArr.splice(this.heroBulletArr.indexOf(bullet), 1);
			Bullet.reclaim(bullet);
		}


		delArr = [];

		for (var i = 0; i < this.enemyBulletArr.length; i++) {
			var bullet:Bullet = this.enemyBulletArr[i];
			bullet.y += speedOffset * SettingManager.shareManger().enemyBulletMoveRate;
			if (bullet.y > this.stage.stageHeight) {
				delArr.push(bullet);
			}
		}

		for (var i = 0; i < delArr[i]; i++) {
			var bullet:Bullet = delArr[i];
			
			this.removeChild(bullet);
			this.enemyBulletArr.splice(this.enemyBulletArr.indexOf(bullet), 1);
			Bullet.reclaim(bullet);
		}

		this.checkHits();

	}


	private checkHits() {

		var delBulletArr:Bullet[] = [];
		var delFighterArr:Plane[] = [];

		// 我的子弹是否打中敌机
		for (var i = 0; i < this.heroBulletArr.length; i++) {

			var heroBullet:Bullet = this.heroBulletArr[i];

			for (var j = 0; j < this.enemyFighterArr.length; j++) {
				
				var fighter:Plane = this.enemyFighterArr[j];

				if (GameUtils.CheckHits(heroBullet, fighter)) {
					if (delFighterArr.indexOf(fighter) == -1) {
						delFighterArr.push(fighter);
					}
					if (delBulletArr.indexOf(heroBullet) == -1) {
						delBulletArr.push(heroBullet);
					}
				}
			}
		}

		for (var i = 0; i < delBulletArr.length; i++) {
			var bullet:Bullet = delBulletArr[i];
			
			this.removeChild(bullet);
			this.heroBulletArr.splice(this.heroBulletArr.indexOf(bullet), 1);
			Bullet.reclaim(bullet);
		}

		for (var i = 0; i < delFighterArr.length; i++) {
			var fighter:Plane = delFighterArr[i];
			fighter.stopFire();
			fighter.removeEventListener("CreateBullet", this.onCreateBullet, this);
			
			this.removeChild(fighter);
			Plane.reclaim(fighter);
			this.enemyFighterArr.splice(this.enemyFighterArr.indexOf(fighter), 1);

			this.sound.play(0, 1);
		}


		// 敌人的子弹是否打中我

		for (var i = 0 ; i < this.enemyBulletArr.length; i++) {
			var bullet:Bullet = this.enemyBulletArr[i];

			if (GameUtils.CheckHits(this.hero, bullet)) {
				this.dispatchEventWith("GameOver");
			}

		}

		// 敌机是否殉身

	}


}